<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :md="4" :sm="8">
            <a-form-item
              label="姓名"
              :maxLength="50">
              <a-input placeholder="请输入姓名" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item
                    label="年级"
                    :maxLength="50">
              <a-select
                      style="width: 100%"
                      placeholder="请选择年级"
                      @change="classChange1"
                      optionFilterProp="children"
                      v-model="queryParam.gradeid">
                <a-select-option
                        v-for="(demo,index) in gradeList"
                        :key="index.toString()"
                        :value="parseInt(demo.id)">
                  {{ demo.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item
                    label="班级"
                    :maxLength="50">
              <a-select
                      style="width: 100%"
                      placeholder="请选择班级"
                      optionFilterProp="children"
                      v-model="queryParam.classId">
                <a-select-option
                        v-for="(demo,index) in classList"
                        :key="index.toString()"
                        :value="parseInt(demo.id)">
                  {{ demo.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" icon="search">查询</a-button>
              <a-button
                @click="() => {this.$objToReset(queryParam),$refs.table.refresh(true)}"
                icon="reload"
                style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <div class="table-operator">
        <a-button @click="$refs.modalEdit.add1(queryParam.schid)" type="primary" icon="plus" v-wx-role="20101">添加</a-button>
        <a-button type="primary" icon="upload" @click="$refs.fileEdit.edit()">导入</a-button>
        <a-button type="primary" icon="query" @click="$refs.errs.edit()">失败查询</a-button>
        <a-button type="primary"><a href='../学生信息导入模板.xls'>模板下载</a></a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item>
              <a-popconfirm
                  title="确定删除吗?"
                  @confirm="() => handleDelete()"
                  >
                <a><a-icon type="delete"/>删除</a>
              </a-popconfirm>
            </a-menu-item>
            <a-menu-item>
              <a @click="handelMc()"><a-icon type="edt"/>学生转班</a>
            </a-menu-item>
            <a-menu-item>
              <a @click="handelQrs()"><a-icon type="edt"/>二维码批量下载</a>
            </a-menu-item>
            <a-menu-item>
              <a @click="handelQrsPt()"><a-icon type="edt"/>二维码批量打印</a>
            </a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">批量操作<a-icon type="down"/></a-button>
        </a-dropdown>
      </div>
      <s-table
        ref="table"
        rowKey="id"
        size="small"
        :scroll="scrollCss"
        :columns="columns"
        :alert="options.alert"
        :row-selection="options.rowSelection"
        :data="loadData">
        <template slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" >
            <a-icon type="edit"/>编辑</a>
          <a-divider type="vertical"/>
          <a @click="handelQr(record)" ><a-icon/> 二维码</a>
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多
              <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
          <a @click="handelView(record)" ><a-icon/> 查看</a>
              </a-menu-item>
              <a-menu-item>
          <a-popconfirm title="确定删除吗?" @confirm="() =>handleDelete(record.id)" >
            <a>删除</a>
          </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </s-table>

      <eye-student-edit ref="modalEdit" @ok="modalEditOk"></eye-student-edit>
      <eye-student-view ref="modalView"></eye-student-view>
      <eye-class-window ref="departCodeRef" @ok="departCodeOk" :multiple-flag="false"></eye-class-window>
      <student-upload ref="fileEdit" @ok="modalEditOk" @close="modalEditOk"></student-upload>
      <eye-errortxt-show ref="errs"></eye-errortxt-show>
      <eye-student-qrcode ref="qr"></eye-student-qrcode>
      <eye-student-move ref="mc" @ok="modalEditOk"></eye-student-move>
      <eye-student-qrcodes ref="qrs" ></eye-student-qrcodes>
      <eye-student-qrcodes-pt ref="qrspt"></eye-student-qrcodes-pt>
    </div>
  </a-card>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import { deleteAction, postAction, getAction} from '@/api/manage'
  import { mixin } from '@/utils/mixin'
  import EyeStudentEdit from './EyeStudentEdit'
  import EyeStudentView from './EyeStudentView'
  import EyeClassWindow from "../eyeClass/EyeClassWindow";
  import StudentUpload from "./studentUpload";
  import EyeErrortxtShow from "../eyeErrortxt/EyeErrortxtShow";
  import EyeStudentQrcode from "./EyeStudentQrcode";
  import EyeStudentMove from "./EyeStudentMove";
  import EyeStudentQrcodes from "./EyeStudentQrcodes";
  import EyeStudentQrcodesPt from "./EyeStudentQrcodesPt";

  export default {
    name: 'EyeStudentList',
    mixins: [mixin],
    components: {
        EyeStudentQrcodesPt,
        EyeStudentQrcodes,
        EyeStudentMove,
        EyeStudentQrcode,
        EyeErrortxtShow, StudentUpload, EyeClassWindow, STable, Ellipsis, EyeStudentEdit, EyeStudentView },
    data () {
      return {
          scrollCss:{x: 'max-content', y: document.body.clientHeight - 280},
        visible: false,
        // 查询条件
        queryParam: {
            name:'',
            classId:'',
            className: ''
        },
        // 表头
        // 如果需要字典，那么需要在columns列中加入 customRender: (text) => this.$sysDictFilter(text, 'xxxx')xxxx 为dataIndex
        columns: [
            {
                title: '姓名',
                dataIndex: 'name',
                width:200
            },
            {
                title: '学校',
                dataIndex: 'schname',
                width:300
            },
            {
                title: '班级',
                dataIndex: 'classname',
                width:320
            },
            {
                title: '编号',
                dataIndex: 'shorter',
                width:150
            },
            {
                title: '性别',
                dataIndex: 'sex',
                width:150,
                customRender: (text) => this.$sysDictFilter(text, 'sex')
            },
           /* {
                title: '年龄',
                dataIndex: 'age',
                width:100
            },
            {
                title: '身份证号',
                dataIndex: 'identity',
                width:150
            },*/
            {
                title: '出生日期',
                dataIndex: 'birthday',
                width:150
            },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          }
        ],
          schList:[],
          gradeList:[],
          classList:[],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          console.log('loadData.parameter', parameter)
          return postAction(this.url.list, Object.assign(parameter, this.queryParam))
              .then(res => {
                  if(res.result.company.schid){
                      this.queryParam.schid = res.result.company.schid
                      this.classChange()
                  }
                return res.result
              })
        },
        selectedRowKeys: [],
        options: {
          alert: {
            clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            type: 'checkbox'
          }
        },
        url: {
          list: '/system/eyeStudent/lists',
          delete: '/system/eyeStudent/delete',
          classList: '/system/eyeClass/getList'
        }
      }
    },
    computed: {},
    methods: {
      // 删除
      handleDelete (record) {
        if (!record) {
          // 如果没有传入，那么批量删除
          record = this.selectedRowKeys.join(',')
        }
        deleteAction(this.url.delete, { ids: record }).then((resp) => {
          this.$message.success('删除成功!')
          this.$refs.table.refresh(true)
        })
      },
      // 用户编辑
      handleEdit (record) {
        this.$refs.modalEdit.edit1(record)
      },
      searchQuery () {
        this.$refs.table.refresh(true)
      },
      // 保存刷新
      modalEditOk () {
        this.$refs.table.refresh()
      },
      // 查看
      handelView (record) {
        this.$refs.modalView.show(record)
      },
        handelQr(record){
            this.$refs.qr.show(record)
        },
        handelMc(){
            const ids = this.selectedRowKeys.join(',')
            this.$refs.mc.add1(this.queryParam.schid,ids)
        },
        handelQrs(){
            const ids = this.selectedRowKeys.join(',')
            this.$refs.qrs.edit(ids)
        },
        handelQrsPt(){
            const ids = this.selectedRowKeys.join(',')
            this.$refs.qrspt.edit(ids)
        },
      //开启选择
      onSelectChange (selectedRowKeys) {
         console.log('selectedRowKeys changed: ', selectedRowKeys)
         this.selectedRowKeys = selectedRowKeys
      },
        // 搜索用户对应的归属部门API
        departCodeSearch () {
            this.$refs.departCodeRef.add([1],2)
        },
        // 获取用户对应部门弹出框提交给返回的数据
        departCodeOk (formData) {
            const departCodeNames = []
            const departCodeKeys = []
            let departCodeNameString = ''
            for (let i = 0; i < formData.departIdList.length; i++) {
                departCodeKeys.push(formData.departIdList[i].key)
                departCodeNames.push(formData.departIdList[i].title)
                departCodeNameString = departCodeNames.join(',')
            }
            if (departCodeKeys.length > 0) {
                this.queryParam.classId = departCodeKeys[0]
                this.queryParam.className = departCodeNameString
            }
        },
        classChange(){
            this.getclasslist(this.queryParam.schid,2)
        },
        classChange1(){
            this.getclasslist(this.queryParam.gradeid,3)
        },
        getclasslist(parentid,orgflag){
            getAction(this.url.classList, { 'parendId': parentid,'orgFlag':orgflag }).then(res => {
                this.$nextTick(() => {
                    if(orgflag==1){
                        this.schList=res.result.data
                    }else if(orgflag==2){
                        this.gradeList = res.result.data
                    }else if(orgflag==3){
                        this.classList = res.result.data
                    }

                })
            })
        },
    },
    watch: {}
  }
</script>
<style scoped>
</style>
